﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace EWDTAssignment.App_Code
{
    public class CRUD
    {
        public static int ExecuteNonQuery(string sql,params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["eShopDB"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand comm = conn.CreateCommand())
                {
                    comm.CommandText = sql;
                    foreach (SqlParameter parameter in parameters)
                    {
                        comm.Parameters.Add(parameter);
                    }
                    return comm.ExecuteNonQuery();
                }
            }
        }
        
        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["eShopDB"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand comm = conn.CreateCommand())
                {
                    comm.CommandText = sql;
                    foreach (SqlParameter parameter in parameters)
                    {
                        comm.Parameters.Add(parameter);
                    }
                    return comm.ExecuteScalar();
                }
            }
        }
        public static DataTable ExecuteReader(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["eShopDB"].ConnectionString))
            {
                conn.Open();
                using (SqlCommand comm = conn.CreateCommand())
                {
                    comm.CommandText = sql;
                    foreach (SqlParameter parameter in parameters)
                    {
                        comm.Parameters.Add(parameter);
                    }
                    DataTable dt = new DataTable();
                    dt.Load(comm.ExecuteReader());
                    return dt;
                }
            }
        }

    }
}